<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/21
 * Time: 16:40
 */
namespace App\Http\Controllers\Api;

use App\Service\Api\CommissionService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Constant\ErrorConstant;

class CommissionController extends BaseController
{
    protected $service;

    public function __construct(Request $request, CommissionService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 用户申请提现
     * @return \Illuminate\Http\JsonResponse
     */
    public function withdrawCommission()
    {
        $validator  =   Validator::make($this->request->post(), [
            'withdraw_money'    =>  'required|numeric|min:1|max:300'
        ], [
            'withdraw_money.required'   =>  '提现金额不能为空',
            'withdraw_money.numeric'    =>  '提现金额必须为数值',
            'withdraw_money.min'        =>  '提现金额最小值为1',
            'withdraw_money.max'        =>  '提现金额最大值为300'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_COMMISSION_WITHDRAW_ERROR, $validator->errors()->first());
        }

        $withdraw_money     =   $this->request->post('withdraw_money');
        $user_info          =   $this->request->post('api_user');
        if ($withdraw_money > $user_info['balance']) {
            return $this->errorResponse(ErrorConstant::API_COMMISSION_WITHDRAW_ERROR, '账户余额不足');
        }

        [$flag, $result]    =   $this->service->commissionWithdraw($withdraw_money);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_COMMISSION_WITHDRAW_ERROR, $result);
        }
    }

    /**
     * 申请进度查看
     * @return \Illuminate\Http\JsonResponse
     */
    public function processCommission()
    {
        $validator  =   Validator::make($this->request->post(), [
            'withdraw_id'    =>  'required|integer'
        ], [
            'withdraw_id.required' =>  '提现ID不能为空',
            'withdraw_id.int'      =>  '提现ID必须为整型'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_COMMISSION_PROCESS_ERROR, $validator->errors()->first());
        }

        $withdraw_id        =   $this->request->post('withdraw_id');

        [$flag, $result]    =   $this->service->commissionProcess($withdraw_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_COMMISSION_PROCESS_ERROR, $result);
        }
    }

    /**
     * 申请记录查看
     * @return \Illuminate\Http\JsonResponse
     */
    public function recordCommission()
    {
        [$flag, $result]    =   $this->service->commissionRecord();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_COMMISSION_RECORD_ERROR, $result);
        }
    }

    /**
     * 佣金记录
     * @return \Illuminate\Http\JsonResponse
     */
    public function totalCommission()
    {
        [$flag, $result]    =   $this->service->commissionTotal();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_COMMISSION_TOTAL_ERROR, $result);
        }
    }

    /**
     * 佣金记录详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function detailCommission()
    {
        $validator  =   Validator::make($this->request->post(), [
            'detail_id'    =>  'required|integer'
        ], [
            'detail_id.required' =>  '提现ID不能为空',
            'detail_id.int'      =>  '提现ID必须为整型'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_COMMISSION_DETAIL_ERROR, $validator->errors()->first());
        }

        $detail_id          =   $this->request->post('detail_id');
        [$flag, $result]    =   $this->service->commissionDetail($detail_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_COMMISSION_DETAIL_ERROR, $result);
        }
    }

}
